var t = getApp(), e = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), a = require("../../0BACB9C3A71FB5DF6DCAD1C4BE4CA4D1.js");

Page({
    data: {
        screenHeight: t.globalData.screenHeight,
        winWidth: t.globalData.windowWidth,
        cartId: "",
        fromPage: "buy",
        cartData: {},
        list: [],
        couponData: null,
        couponStatus: 2,
        totalCoupon: 0,
        addressData: null,
        typeTaking: null,
        shoppingMoney: "0.00",
        getType: 1,
        priceObj: {},
        goodsIds: "",
        nearByTipsType: 0,
        discountAmount: "0.00",
        freightPrice: "0.00",
        getTypeAnimData: {},
        deviceList: [],
        currentDeviceId: null,
        deviceLoading: !1,
        decalsPrice: "0.00",
        isNoData: !1
    },
    onLoad: function(t) {
        var e = this, a = t.cartId;
        this.setData({
            cartId: a,
            fromPage: t.from || "buy"
        }, function() {
            e.getCartData(a);
        });
    },
    onUnload: function() {
        t.globalData.chooseCouponItem = null, t.globalData.chooseAddressItem = null;
    },
    onReady: function() {
        this.getTypeAnim = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-in"
        }), this.payTip = this.selectComponent("#payTip");
    },
    onShow: function() {
        var e = this, a = {}, o = t.globalData.chooseAddressItem;
        a = o ? {
            addressData: o
        } : {
            addressData: null
        };
        var i = t.globalData.chooseCouponItem;
        i ? a.couponData = i : (a.couponData = null, a.discountAmount = "0.00"), this.setData(a, function() {
            var t = e.data.couponStatus;
            1 === t && e.getCouponCount(), 1 === t && i && 1 * e.data.shoppingMoney != 0 && e.getAmount();
        });
    },
    getCartData: function(a) {
        var o = this;
        wx.showLoading({
            title: "加载中...",
            mask: !0
        }), e.get("/v2/shopping/submitOrder", {
            id: a
        }).then(function(e) {
            if (wx.hideLoading(), 1001 === e.data.code) {
                var a = e.data.response.data, i = a.type_taking, n = a.coupon_status, d = a.coupon, s = a.address, c = a.shopping_cart;
                t.globalData.chooseAddressItem = s.id ? s : null, t.globalData.chooseCouponItem = d.coupon_id ? d : null;
                var r = 0, u = [], l = {}, p = !0, g = !1, h = void 0;
                try {
                    for (var v, D = c[Symbol.iterator](); !(p = (v = D.next()).done); p = !0) {
                        var f = v.value;
                        r += f.total_media_drew_cent * f.goods_num;
                        var y = 100 * f.product_price_discount_cent;
                        l[f.goods_id] || u.push(f.goods_id), (!l[f.goods_id] || l[f.goods_id] < y) && (l[f.goods_id] = y);
                    }
                } catch (t) {
                    g = !0, h = t;
                } finally {
                    try {
                        !p && D.return && D.return();
                    } finally {
                        if (g) throw h;
                    }
                }
                r += r % 1 == 0 ? ".00" : "", o.setData({
                    cartData: a,
                    list: c,
                    goodsIds: u.join(","),
                    priceObj: l,
                    decalsPrice: r,
                    couponData: d.coupon_id ? d : null,
                    addressData: s.id ? s : null,
                    couponStatus: n,
                    typeTaking: i,
                    getType: 2 === i || 3 === i ? 1 : 2,
                    shoppingMoney: a.shopping_money
                }, function() {
                    1 === n && o.getCouponCount(), 1 === n && d.coupon_id && o.getAmount();
                });
            }
        }).catch(function() {
            wx.hideLoading();
        });
    },
    getAmount: function() {
        var a = this, o = t.globalData.chooseCouponItem;
        o && e.post("/v2/coupon/discount", {
            goods: JSON.stringify(this.data.priceObj),
            coupon_id: o.coupon_id
        }).then(function(t) {
            1001 === t.data.code && a.setData({
                discountAmount: t.data.response.data.discount_amount
            });
        });
    },
    getCouponCount: function() {
        var t = this;
        a.getCouponCount(function(e) {
            if (1001 === e.data.code) {
                var a = 1 * e.data.response.data.count;
                t.setData({
                    totalCoupon: a
                });
            }
        });
    },
    submitOrder: function() {
        var t = this, e = this.data.getType;
        if (0 !== e || this.data.currentDeviceId) if (1 !== e || this.data.addressData) if (0 === e) this.payTip.getDeviceMsg({
            deviceId: this.data.currentDeviceId,
            price: (1e5 * this.data.shoppingMoney - 1e5 * this.data.discountAmount) / 1e5,
            isFeedback: !0
        }); else {
            var a = Date.now(), o = new Date("2019/09/30 16:00:00").getTime(), i = new Date("2019/10/07 00:00:00").getTime();
            a >= o && a <= i ? this.setData({
                isShowActivityTip: !0
            }) : wx.showModal({
                title: "提示",
                content: "是否提交订单？",
                success: function(e) {
                    e.confirm && t.createOrder();
                }
            });
        } else this.goToMyAddress(); else wx.showToast({
            title: "请选择取货店面！",
            icon: "none"
        });
    },
    createOrder: function() {
        var a = this, o = this.data.getType, i = {
            shopping_id: this.data.cartId,
            type_taking: o
        }, n = this.data.addressData;
        1 === o && n && (i.address_id = 1 * n.address_id || 1 * n.id);
        var d = this.data.couponData;
        d && (i.coupon_id = d.coupon_id), 0 === o && (i.device_id = this.data.currentDeviceId), 
        1 === o && t.globalData.deviceId && (i.device_id = t.globalData.deviceId), wx.showLoading({
            mask: !0
        }), e.post("/v2/order/product/create", i).then(function(e) {
            if (wx.hideLoading(), 1001 == e.data.code) {
                "cart" === a.data.fromPage && a.removeProductFromCart();
                var o = e.data.response.data;
                t.globalData.chooseCouponItem = null, wx.redirectTo({
                    url: "/pages/pay/pay?orderId=" + o.order_id + "&price=" + o.order_total_paid
                });
            }
        }).catch(function() {
            wx.hideLoading();
        });
    },
    removeProductFromCart: function() {
        this.data.cartId.split(",");
        var e = t.globalData.cartPageData, a = e.list, o = [], i = {}, n = !0, d = !1, s = void 0;
        try {
            for (var c, r = a[Symbol.iterator](); !(n = (c = r.next()).done); n = !0) {
                var u = c.value;
                i[u.id] || o.push(u);
            }
        } catch (t) {
            d = !0, s = t;
        } finally {
            try {
                !n && r.return && r.return();
            } finally {
                if (d) throw s;
            }
        }
        e.list = o, t.globalData.isNeedToUpdateCartData = !0, t.globalData.cartPageData = e;
    },
    goToMyAddress: function() {
        wx.navigateTo({
            url: "/pages/address/index?from=order"
        });
    },
    goToMyCoupon: function() {
        wx.navigateTo({
            url: "/pages/coupon/index?from=order&ids=" + this.data.goodsIds
        });
    },
    changeGetType: function(t) {
        var e = this, a = 1 * t.currentTarget.dataset.getType;
        a !== this.data.getType && this.setData({
            getType: a
        }, function() {
            e.hideGetTypeBox(!1), 0 === e.data.getType && e.loadNearByDevice();
        });
    },
    loadNearByDevice: function() {
        var t = this;
        this.setData({
            deviceLoading: !0
        }), wx.getLocation({
            type: "gcj02",
            success: function(a) {
                console.log(a), e.get("/v2/device/nearby", {
                    current_longitude: a.longitude,
                    current_latitude: a.latitude,
                    range: 10
                }).then(function(e) {
                    if (t.setData({
                        deviceLoading: !1
                    }), 1001 === e.data.code) {
                        var a = e.data.response.data;
                        t.setData({
                            deviceList: a,
                            currentDeviceId: a[0] ? a[0].device_id : null,
                            isNoData: !a.length
                        });
                    } else t.setData({
                        deviceList: [],
                        currentDeviceId: null
                    });
                }).catch(function() {
                    t.setData({
                        deviceList: [],
                        currentDeviceId: null,
                        deviceLoading: !1
                    });
                });
            },
            fail: function() {
                t.setData({
                    deviceLoading: !1
                }), wx.hideLoading(), wx.showModal({
                    title: "提示",
                    content: "需要允许使用您的地理位置",
                    success: function(e) {
                        e.confirm && wx.openSetting({
                            success: function(e) {
                                e.authSetting["scope.userLocation"] && t.loadNearByDevice();
                            }
                        });
                    }
                });
            }
        });
    },
    changeDevice: function(t) {
        this.setData({
            currentDeviceId: 1 * t.currentTarget.dataset.id
        });
    },
    hideGetTypeBox: function() {
        this.handleGetTypeAnim(!1);
    },
    showGetTypeBox: function() {
        this.handleGetTypeAnim(!0);
    },
    handleGetTypeAnim: function(t) {
        var e = -this.data.screenHeight;
        t && (e = 0), this.getTypeAnim.bottom(e).step(), this.setData({
            getTypeAnimData: this.getTypeAnim.export()
        });
    }
});